<?php
#header("Content-Type: text/html; charset=iso-8859-1");
require_once('include.php');
function form_ident(){
	echo "<form name = 'myform' action = 'javascript: identify(document.myform.nom.value,document.myform.ddn.value)' method = 'post' />\n";
	echo "<p>Nom de famille : <br /><input type = 'text' size = '15' name = 'nom' /></p>\n";
	echo "<p>Date de naissance (jjmmaaaa): <br /><input type = 'password' size = '15' name = 'ddn' />\n";
	echo "<p class = 'center'><button type = 'submit'>Identifiez-vous</button></p></form>\n";
}
function form_admin(){
	$membre = new membre(check_cookie());
	$q = "select id from commissions where nom = 'comite'";
	$r = ExecRequete($q);
	$id_comite = ($s = LigneSuivante($r)) ? $s->id : '';
	$texte = (array_key_exists($id_comite,$membre->commissions)) ? "du comit&eacute;" : "de la gestion du VBCLL";
	$title = "Identification d'un membre $texte";
	$corps = "<form name = 'myform' action = 'javascript: identify_admin(document.myform.password.value)' method = 'post' />\n";
	$corps .= "<p class = 'center'>Veuillez entrer le mot de passe ultra-secret $texte<br /><input type = 'password' size = '15' name = 'password' />\n";
	$corps .= "<p class = 'center'><button type = 'submit' name = 'submit' value = '1' >Identifiez-vous</button></p></form>\n";
	echo "<div style = 'position: relative; width: 450px; margin-left: auto; margin-right: auto'>\n";
	echo box($title,$corps,450,1);
	echo "</div>\n";
}
if($user_id = check_cookie()){
	if(!empty($_REQUEST['action'])){
		if($_REQUEST['action'] == 'del'){
			$param = time() -3600;
			setcookie("VBCLL2","",$param);
			form_ident();
		}
		elseif($_REQUEST['action'] == 'admin'){
			$membre = new membre($user_id);
			if(count($membre->commissions)){
				entete('VBCLL',"","",1);
				form_admin();
				pied();
			}
			else redirect('perso.php?action=admin');
		}
		elseif($_REQUEST['action'] == 'valide_admin'){
			$password = (!empty($_REQUEST['password'])) ? $_REQUEST['password'] : "";
			$membre = new membre($user_id);
			$q = "select id from commissions where nom = 'comite'";
			$r = ExecRequete($q);
			$id_comite = ($s = LigneSuivante($r)) ? $s->id : '';

			if($password == PWD_COMITE && array_key_exists($id_comite,$membre->commissions)){
				$param = time() +  86400*365;
				$cookie = $user_id.":".($user_id*strlen($membre->nom)+strlen($membre->prenom)*15698);
				setcookie("VBCLLADMIN",$cookie,$param);
				echo 1;
			}
			elseif(count($membre->commissions)){
				$q = "select id,nom from commissions where nom != 'comite'";
				$r = ExecRequete($q);
				while($s = LigneSuivante($r)){
					if($s->nom == 'commission_technique'){
						if($password == PWD_TECHNIQUE && array_key_exists($s->id,$membre->commissions)){
							$param = time() +  86400*365;
							$cookie = $user_id.":".($user_id*strlen($membre->nom)+strlen($membre->prenom)*15698);
							setcookie("VBCLLADMIN",$cookie,$param);
							echo 1;
							break;
						}
					}
					elseif($s->nom == "commission_LN"){
						if($password == PWD_LN && array_key_exists($s->id,$membre->commissions)){
							$param = time() +  86400*365;
							$cookie = $user_id.":".($user_id*strlen($membre->nom)+strlen($membre->prenom)*15698);
							setcookie("VBCLLADMIN",$cookie,$param);
							echo 1;
							break;
						}
					}
					elseif($s->nom == "commission_arb_marq"){
						if($password == PWD_ARB_MARQ && array_key_exists($s->id,$membre->commissions)){
							$param = time() +  86400*365;
							$cookie = $user_id.":".($user_id*strlen($membre->nom)+strlen($membre->prenom)*15698);
							setcookie("VBCLLADMIN",$cookie,$param);
							echo 1;
							break;
						}
					}
				}
			}

			else echo 0;
		}
	}
	else{
		$membre = new membre($user_id);
		echo "<p class ='left' style = 'padding: 0; margin: 0; line-height: 8pt; margin-top: -6pt;'>";
		echo "<a href = 'membre.php?id=$user_id&action=complet'>ma page</a>&nbsp;&nbsp;|&nbsp;&nbsp;";
		echo "<a href = 'javascript:list_communications()'>communications</a></p>";
		echo $membre->agenda();
		echo "<!----><span class = 'logout'>(<a href = 'javascript:logout()'>pas ".(CapitalizeLastName(strtolower($membre->prenom)))."</a>)</span>\n";
	}
}
else{
	if(!empty($_REQUEST['action'])){
		$action = $_REQUEST['action'];
		if($action == 'submit'){
			$nom = (!empty($_REQUEST['nom'])) ? $_REQUEST['nom'] : "";
			$ddn = (!empty($_REQUEST['ddn'])) ? $_REQUEST['ddn'] : "";
			$ddn3 = ereg_replace("[^0-9]","",$ddn);
			$ddn1 = substr($ddn3,4,4)."-".substr($ddn3,2,2)."-".substr($ddn3,0,2);
			$ddn2 = substr($ddn3,0,4)."-".substr($ddn3,4,2)."-".substr($ddn3,6,2);
			$q = "select id from membres where nom = '".(addslashes($nom))."' and (ddn = '$ddn2' or ddn = '$ddn1')";
			$r = ExecRequete($q);
			if($s = LigneSuivante($r)){
				$membre = new membre($s->id);
				$param = time() +  86400*365;
				$check = $s->id * strlen($membre->nom) + strlen($membre->prenom);
				$cookie = $s->id.":".$check;
				setcookie("VBCLL2",$cookie,$param);
				echo $membre->agenda();
				echo "<p class ='center' style = 'padding: 0; margin: 0; line-height: 8pt;'><a href = 'javascript:list_communications()'>communications</a>&nbsp;&nbsp;";
				echo "<a href = 'membre.php?id=$s->id&action=complet'>coordonn&eacute;es</a></p>";
				echo "<!----><span class = 'logout'>(<a href = 'javascript:logout()'>pas ".(CapitalizeLastName(strtolower($membre->prenom)))."</a>)</span>\n";
				echo "<!---->$membre->prenom";
				echo "<!---->$cookie";
			}
			else{
				echo "KO<!---->";
				form_ident();
			}
		}
	}
	else{
		form_ident();
	}
}
?>